package com.ricky.mq.client.message;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.ricky.mq.client.message.model.BoringMsgModel;
import com.ricky.mq.client.message.model.BoringQueue;
import com.ricky.mq.client.message.model.BoringTopic;

/**
 * 文本消息
 *
 * @author ricky
 * @since 2020.06.18
 */
public class TextMsg implements BoringMessage {

    /**
     * 队列
     */
    private BoringQueue queue;

    /**
     * 主题
     */
    private BoringTopic topic;

    /**
     * 消息
     */
    private String message;

    /**
     * 0队列，1主题
     */
    private int msgType;

    /**
     * 唯一表示
     */
    private String uuid;

    /**
     * 消息模型名
     */
    private String modelMsgName;

    public TextMsg(BoringQueue queue) {
        this.queue = queue;
        msgType = BoringMsgModel.QUEUE;
    }

    public TextMsg(BoringTopic topic) {
        this.topic = topic;
        msgType = BoringMsgModel.TOPIC;
    }

    @Override
    public boolean send() {
        switch (msgType) {
            case BoringMsgModel.QUEUE:
                return this.queue.getBoringSession().doSend(this, this.queue);
            case BoringMsgModel.TOPIC:
                return this.topic.getBoringSession().doSend(this, this.topic);
            default:
                return false;
        }
    }

    @Override
    public void setMessage(Object message) {
        if (ObjectUtil.isNull(message)) {
            throw new RuntimeException("消息不能为NULL！");
        }
        if (message instanceof String) {
            setTextMsg(Convert.toStr(message));
        } else {
            setTextMsg(JSONUtil.parseObj(message).toString());
        }
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public int getMsgType() {
        return this.msgType;
    }

    @Override
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Override
    public String getUuid() {
        return this.uuid;
    }

    public void setTextMsg(String message) {
        this.message = message;
    }

    public String getModelMsgName() {
        return modelMsgName;
    }

    public void setModelMsgName(String modelMsgName) {
        this.modelMsgName = modelMsgName;
    }
}
